package com.paipaideli.ui.mine.invitation;

import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.paipaideli.BuildConfig;
import com.paipaideli.R;
import com.paipaideli.common.base.BasePresenterActivity;
import com.paipaideli.common.bottomsheet.ShareDialogFragment;
import com.paipaideli.common.utils.ImageUtil;
import com.paipaideli.common.utils.ToastUtil;
import com.paipaideli.common.utils.ZXingUtils;
import com.paipaideli.common.utils.preferrnces.FastData;
import com.paipaideli.common.views.CircleImageView;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by 邀请
 */
public class InvitationActivity extends BasePresenterActivity implements UMShareListener {

    Unbinder unbinder;
    @BindView(R.id.top_back)
    RelativeLayout top_back;
    @BindView(R.id.top_title)
    TextView top_title;
    @BindView(R.id.textView88)
    TextView textView88;
    @BindView(R.id.tv_invitation_name)
    TextView tv_invitation_name;
    @BindView(R.id.btn_invitation)
    Button btn_invitation;
    @BindView(R.id.image_invitation)
    ImageView image_invitation;
    @BindView(R.id.imageView50)
    CircleImageView imageView50;

    private UMImage image;
    private ShareDialogFragment dialogFragment;

    @Override
    public int getContentView() {
        return R.layout.activity_invitation;
    }

    @Override
    public void initView() {
        unbinder = ButterKnife.bind(this);
        top_title.setText("我的邀请");
        //返回
        top_back.setOnClickListener(o ->
                finish()
        );
        textView88.setText(FastData.getReferralCode());
        String image = getIntent().getStringExtra("image");
        String uesrname = getIntent().getStringExtra("name");
        tv_invitation_name.setText(uesrname);
        if (!TextUtils.isEmpty(image)) {
            ImageUtil.displayImage(image, imageView50);
        } else {
            imageView50.setBackgroundResource(R.mipmap.login_logo);
        }

        Bitmap bitmap = ZXingUtils.createQRImage(BuildConfig.h5 + "register.html?id=" + FastData.getReferralCode(), 1000, 1000);
        image_invitation.setImageBitmap(bitmap);
        dialogFragment = ShareDialogFragment.newInstance();
        btn_invitation.setOnClickListener(v -> {

            ShareAction shareAction = new ShareAction(InvitationActivity.this);
            shareAction.withText("拍拍得利");
            UMWeb umWeb = new UMWeb(BuildConfig.h5 + "register.html?id=" + FastData.getReferralCode());
            umWeb.setTitle(uesrname + "邀请你来玩拍拍得利。拍中省钱,不中赚钱！");
            umWeb.setThumb(this.image);
            umWeb.setDescription("在这里，真正的议价权属于每个平凡的人，让用户定价，竞拍，赋予电商全新的参与和竞技趣味。");
            shareAction.withMedia(umWeb);

            shareAction.setPlatform(SHARE_MEDIA.WEIXIN);
            shareAction.setCallback(this);
            shareAction.share();
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onStart(SHARE_MEDIA share_media) {

    }

    @Override
    public void onResult(SHARE_MEDIA share_media) {
        if (share_media.name().equals("WEIXIN_FAVORITE")) {
            ToastUtil.show("收藏成功");
        } else {
            ToastUtil.show("分享成功");
        }
    }

    @Override
    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
        ToastUtil.show("分享失败");
    }

    @Override
    public void onCancel(SHARE_MEDIA share_media) {
        ToastUtil.show("分享取消");
    }
}
